package com.lepu.app.fun.tool.szqx;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.app.config.UserConfig;
import com.app.utils.Const;
import com.app.utils.DateUtil;
import com.app.utils.LoginUtil;
import com.app.utils.Setting;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.ApiClient;
import com.core.lib.core.AsyncRequest;
import com.core.lib.ui.wheel.WheelView;
import com.core.lib.utils.DateType;
import com.core.lib.utils.main.DateUtilBase;
import com.core.lib.utils.main.UIHelper;
import com.core.lib.utils.main.UtilityBase;
import com.core.lib.widget.MyDialog;
import com.eyzhs.app.R;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;

public class GrowBabyActivity extends BaseFragmentActivity implements
		OnTopbarNewLeftLayoutListener, AsyncRequest {
	private String REQUEST_DATA = "request_data";
	private TextView mBabyDateEdit;
	
	private TextView mManTextView;
	private TextView mWmanTextView;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {

		mNeedStatusBar = false;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tool_grow_baby_activity);

		UtilityBase.setStatusBarRes(this, R.color.color_bmi, true);
		init();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish(true);
	}

	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}

	/**
	 * 初始化方法
	 */
	private void init() {
		// topbar
		CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
		topbar.setTopbarTitle("宝宝资料");
		topbar.setTopbarBackground(R.color.color_bmi);
		topbar.setonTopbarNewLeftLayoutListener(this);

		mBabyDateEdit = (TextView) findViewById(R.id.babydateEditText);
		mManTextView = (TextView) findViewById(R.id.yesText);
		mWmanTextView = (TextView) findViewById(R.id.noText);
		
		mBabyDateEdit.setOnClickListener(onClick);
		mManTextView.setOnClickListener(onClick);
		mWmanTextView.setOnClickListener(onClick);
		
		String babyDate = UserConfig.getConfigString(GrowBabyActivity.this, Const.CONFIG_APP_BABY_DATE, "");
		if(!TextUtils.isEmpty(babyDate)){
			mBabyDateEdit.setText(babyDate);
		}
		
		int babySex = UserConfig.getConfigInt(GrowBabyActivity.this, Const.CONFIG_APP_BABY_SEX, 1);
		if(babySex == 1){
			mManTextView.setSelected(true);
		}else if(babySex == 2){
			mWmanTextView.setSelected(true);
		}
		
	}

	//上传
	public void request(String babyDate, int sex ) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		JSONObject params = new JSONObject();
		try {
			params.put("DeviceID", Setting.DEVICEID);
			params.put("ApplicationID", Setting.APPLICATIONID);
			
			params.put("UserID", LoginUtil.getUserId());
			params.put("LoginToken", LoginUtil.getLoginToken());
			params.put("ToolID", 1);
			params.put("BabyBirthday", babyDate);
			params.put("BabyGender", sex);

		} catch (Exception e) {
		}
		map.put("ht", params);

		ApiClient.http_post_main(Setting.getUploadYiMiaoUrl(), map, null, this,REQUEST_DATA, true, false);
	}

	@SuppressLint("DefaultLocale")
	public void viewClick(View view) {
		int rid = view.getId();
		switch (rid) {
		case R.id.testButton:
            String babyDate = mBabyDateEdit.getText().toString().trim();
            int sex = 1;
            
			if (TextUtils.isEmpty(babyDate)) {
				UIHelper.showToast(this, "请完整填写 宝宝出生日期");
				return;
			}else{
				UserConfig.setConfig(GrowBabyActivity.this, Const.CONFIG_APP_BABY_DATE, babyDate);
				if(mManTextView.isSelected()){
					//男
					UserConfig.setConfig(GrowBabyActivity.this, Const.CONFIG_APP_BABY_SEX, 1);
					sex = 1;
				}else{
					//女
					UserConfig.setConfig(GrowBabyActivity.this, Const.CONFIG_APP_BABY_SEX, 2);
					sex = 2;
				}
				
				request(babyDate, sex);
				showProgressDialog();
			}
			
			break;

		default:
			break;
		}
	}
	
	View.OnClickListener onClick = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			
			case R.id.babydateEditText:
				showBirthDateDialog(mBabyDateEdit);
				break;
			case R.id.yesText:
				mManTextView.setSelected(true);
				mWmanTextView.setSelected(false);
				break;
			case R.id.noText:
				mManTextView.setSelected(false);
				mWmanTextView.setSelected(true);
				break;
			default:
				break;
			}
		}
	};

	private void showBirthDateDialog(final TextView textView) {

		String babyDate = UserConfig.getConfigString(GrowBabyActivity.this, Const.CONFIG_APP_BABY_DATE, "");
		String dateStr = "";
		if (TextUtils.isEmpty(babyDate)) {
			dateStr = DateUtilBase.stringFromDate(new Date(),DateUtilBase.YEAR_MONTH_DAY);
		} else {
			Date date = DateUtilBase.dateFromString(babyDate,DateUtilBase.YEAR_MONTH_DAY);
			dateStr = DateUtilBase.stringFromDate(date,DateUtilBase.YEAR_MONTH_DAY);
		}

		Date date = DateUtilBase.dateFromString(dateStr,DateUtilBase.YEAR_MONTH_DAY);
		MyDialog dialog = new MyDialog(GrowBabyActivity.this)
				.setTitle("请选择宝宝出生日期")
				.setDateTimePicker(date, DateType.YEAR_MONTH_DAY)
				.setNegativeButton(R.string.app_cancel,
						new View.OnClickListener() {

							@Override
							public void onClick(View arg0) {

							}
						})
				.setPositiveButton(R.string.app_ok, new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// 如果是个数,则显示为"02"的样式
						WheelView wv_year = (WheelView) v
								.findViewById(R.id.year);
						WheelView wv_month = (WheelView) v
								.findViewById(R.id.month);
						WheelView wv_day = (WheelView) v.findViewById(R.id.day);

						String parten = "00";
						DecimalFormat decimal = new DecimalFormat(parten);
						// 设置日期的显示
						String text = (wv_year.getCurrentItem() + MyDialog.START_YEAR)
								+ "-"
								+ decimal.format((wv_month.getCurrentItem() + 1))
								+ "-"
								+ decimal.format((wv_day.getCurrentItem() + 1));
						
						if(DateUtil.getLongTime(text) > DateUtil.getLongTime(DateUtil.stringFromDate(new Date(), DateUtilBase.YEAR_MONTH_DAY))){
							UIHelper.showToast(GrowBabyActivity.this, "不能选择未来时间");
						}else{
							textView.setText(text);
						}

						textView.setText(text);

					}
				});

		dialog.create(null).show();
	}
	
	
	
	@Override
	public void RequestComplete(Object arg0, Object arg1) {
		hideProgressDialog();
		finish(true);
	}

	@Override
	public void RequestError(Object arg0, int arg1, String arg2) {
		hideProgressDialog();
		finish(true);
	}

}
